﻿package glam;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.Shape;
import flash.filters.BevelFilter;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.Lib;
import fpc.utils.ScaledBitmap;

class Primitives 
{
	public static var defaultPrimitives(getDefaultPrimitives, setDefaultPrimitives):Primitives;
	static var _defaultPrimitives:Primitives;
	static function getDefaultPrimitives()
	{
		if (_defaultPrimitives == null)
		{
			_defaultPrimitives = new Primitives();
		}
		return _defaultPrimitives;
	}
	static function setDefaultPrimitives(value)
	{
		_defaultPrimitives = value;
		return value;
	}
	
	static var DEFAULT_WIDTH = 25;
	
	static var DEFAULT_HEIGHT = 50;
	
	static var DEFAULT_STRIAE_SIZE = 100;
	
	static var _shape = new Shape();
	
	public var buttonBackgroundColors:Array<UInt>;
	
	public var buttonBackgroundDisabledColors:Array<UInt>;
	
	public var buttonBackgroundTopColor:UInt;
	
	public var buttonBackgroundTopAlpha:Float;
	
	public var outerGlowColor:UInt;
	
	public var innerGlowColor:UInt;
	
	public var textBackgroundColor:UInt;
	
	public var textBackgroundColorDisabled:UInt;
	
	public var textOuterGlowColor:UInt;
	
	public var textInnerGlowColor:UInt;
	
	public var roundPointColor:UInt;
	
	public var roundPointColorDisabled:UInt;
	
	public var roundTopColor:UInt;
	
	public var roundTopAlpha:Float;
	
	public var textColor:UInt;
	
	public var textColorDisabled:UInt;
	
	public function new()
	{
		buttonBackgroundColors = [0xfff040, 0xffc010, 0xffa000, 0xffe030];
		buttonBackgroundDisabledColors = [0xe0e0a5, 0xf0f0a0, 0xe0e0a0, 0xe0e0a0];
		buttonBackgroundTopColor = 0xfff0e0;
		buttonBackgroundTopAlpha = .7;
		
		outerGlowColor = 0x000000;
		innerGlowColor = 0xffffff;
		
		textBackgroundColor = 0xffffff;
		textBackgroundColorDisabled = 0xe0e0e0;
		textOuterGlowColor = 0xffffff;
		textInnerGlowColor = 0x000000;
		
		roundPointColor = 0x000000;
		roundPointColorDisabled = 0xa0a0a0;
		roundTopColor = 0xffffff;
		roundTopAlpha = .8;
		
		textColor = 0x000000;
		textColorDisabled = 0x707070;
	}
	
	function getGradientBackground(colors:Array<UInt>, innerStrength:Float, outerStrength:Float)
	{
		var g = _shape.graphics;
		g.clear();
		
		var matrix = new Matrix();
		matrix.createGradientBox(DEFAULT_HEIGHT, DEFAULT_HEIGHT, Math.PI / 2);
		
		g.lineStyle();
		g.beginGradientFill(GradientType.LINEAR, colors, [1, 1, 1, 1], [0, 125, 130, 255], matrix);
		g.drawRoundRect(2, 2, DEFAULT_WIDTH - 4, DEFAULT_HEIGHT - 4, 4);
		g.endFill();
		
		var bitmapData = new BitmapData(DEFAULT_WIDTH, DEFAULT_HEIGHT, true, 0x0);
		bitmapData.draw(_shape);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(innerGlowColor, 1, 2, 2, innerStrength, BitmapFilterQuality.HIGH, true)
		);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(outerGlowColor, 1, 2, 2, outerStrength, BitmapFilterQuality.HIGH)
		);
		return bitmapData;
	}
	
	public function getButtonBackground():BitmapData
	{
		return getGradientBackground(buttonBackgroundColors, 1.7, 1.5);
	}
	
	public function getButtonBackgroundDisabled():BitmapData
	{
		return getGradientBackground(buttonBackgroundDisabledColors, .7, .5);
	}
	
	public function getTextBackground()
	{
		var innerStrength = 1.7;
		var outerStrength = .5;
		
		var g = _shape.graphics;
		g.clear();
		
		g.beginFill(textBackgroundColor);
		g.drawRoundRect(2, 2, DEFAULT_WIDTH - 4, DEFAULT_HEIGHT - 4, 4);
		g.endFill();
		
		var bitmapData = new BitmapData(DEFAULT_WIDTH, DEFAULT_HEIGHT, true, 0x0);
		bitmapData.draw(_shape);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(textInnerGlowColor, 1, 2, 2, innerStrength, BitmapFilterQuality.HIGH, true)
		);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(textOuterGlowColor, 1, 2, 2, outerStrength, BitmapFilterQuality.HIGH)
		);
		return bitmapData;
	}
	
	public function getKnockoutShape()
	{
		var innerStrength = 1.7;
		var outerStrength = 1.5;
		
		var g = _shape.graphics;
		g.clear();
		
		g.beginFill(outerGlowColor, .1);
		g.drawRoundRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT, 8);
		g.drawRoundRect(1, 1, DEFAULT_WIDTH - 2, DEFAULT_HEIGHT - 2, 6);
		g.endFill();
		
		g.beginFill(outerGlowColor, .5);
		g.drawRoundRect(1, 1, DEFAULT_WIDTH - 2, DEFAULT_HEIGHT - 2, 6);
		g.drawRoundRect(2, 2, DEFAULT_WIDTH - 4, DEFAULT_HEIGHT - 4, 4);
		g.endFill();
		
		g.beginFill(innerGlowColor, .5);
		g.drawRoundRect(2, 2, DEFAULT_WIDTH - 4, DEFAULT_HEIGHT - 4, 4);
		g.drawRoundRect(4, 4, DEFAULT_WIDTH - 8, DEFAULT_HEIGHT - 8, 4);
		g.endFill();
		
		var bitmapData = new BitmapData(DEFAULT_WIDTH, DEFAULT_HEIGHT, true, 0x0);
		bitmapData.draw(_shape);
		return bitmapData;
	}
	
	function getStriae(color:UInt, backgroundColor:UInt, space:Int, size:Int)
	{
		var g = _shape.graphics;
		g.clear();
		
		g.lineStyle(size, color);
		var x = -DEFAULT_STRIAE_SIZE;
		while (x < DEFAULT_STRIAE_SIZE)
		{
			g.moveTo(x + DEFAULT_STRIAE_SIZE, 0);
			g.lineTo(x, DEFAULT_STRIAE_SIZE);
			x += space + size;
		}
		
		var bitmapData = new BitmapData(DEFAULT_STRIAE_SIZE, DEFAULT_STRIAE_SIZE, false, backgroundColor);
		bitmapData.draw(_shape);
		return bitmapData;
	}
	
	public function getPanelStriae()
	{
		return getStriae(0xc0c0c0, 0xd0d0d0, 3, 1);
	}
	
	static var ROUND_SIZE = 18;
	
	public function getToggleShape(isRound:Bool, enabled:Bool)
	{
		var innerStrength = 1.7;
		var outerStrength = .5;
		
		var g = _shape.graphics;
		g.clear();
		
		var halfRoundSize = ROUND_SIZE >> 1;
		
		g.beginFill(enabled ? textBackgroundColor : textBackgroundColorDisabled);
		if (isRound)
		{
			g.drawCircle(halfRoundSize, halfRoundSize, halfRoundSize - 3);
		}
		else
		{
			g.drawRect(2, 2, ROUND_SIZE - 4, ROUND_SIZE - 4);
		}
		g.endFill();
		
		var bitmapData:BitmapData = new BitmapData(ROUND_SIZE, ROUND_SIZE, true, 0x0);
		bitmapData.draw(_shape);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(textInnerGlowColor, 1, 2, 2, innerStrength, BitmapFilterQuality.HIGH, true)
		);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(textOuterGlowColor, 1, 3, 3, outerStrength, BitmapFilterQuality.HIGH)
		);
		return bitmapData;
	}
	
	function getScrollBackground(down:Bool)
	{
		var defaultScrollThickness = 18;
		
		var g = _shape.graphics;
		g.clear();
		
		g.beginFill(down ? 0xc0c0c0 : 0xa1a1a1);
		g.drawRect(0, 0, DEFAULT_WIDTH, defaultScrollThickness);
		g.endFill();
		g.lineStyle(1, 0x8f8f8f);
		var y = 0;
		while (y <= defaultScrollThickness)
		{
			g.moveTo(0, y);
			g.lineTo(DEFAULT_WIDTH, y);
			y += 2;
		}
		
		var bitmapData = new BitmapData(DEFAULT_WIDTH, defaultScrollThickness, true, 0x00efefef);
		bitmapData.draw(_shape);
		return bitmapData;
	}
	
	public function getScrollBackgroundUp()
	{
		return getScrollBackground(false);
	}
	
	public function getScrollBackgroundDown()
	{
		return getScrollBackground(true);
	}
	
	public function getThumbIcon()
	{
		var defaultScrollThickness = 18;
		
		var g = _shape.graphics;
		g.clear();
		
		for (i in 0 ... 4)
		{
			g.beginFill(0x000000, .4);
			g.drawRect(2 + i * 3, 3, 2, defaultScrollThickness - 6);
			g.endFill();
		}
		
		var innerStrength = 1.7;
		
		var bitmapData = new BitmapData(defaultScrollThickness - 4, defaultScrollThickness - 2, true, 0x00efefef);
		bitmapData.draw(_shape);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(0xffffff, 1, 2, 2, innerStrength, BitmapFilterQuality.HIGH)
		);
		return bitmapData;
	}
	
	public function getArrowIcon()
	{
		var g = _shape.graphics;
		g.clear();
		
		g.beginFill(0x000000, .6);
		g.moveTo(3, 1);
		g.lineTo(8, 7);
		g.lineTo(3, 13);
		g.lineTo(1, 11);
		g.lineTo(4, 7);
		g.lineTo(1, 3);
		g.endFill();
		
		var innerStrength = 1.7;
		
		var bitmapData = new BitmapData(12, 14, true, 0x00efefef);
		bitmapData.draw(_shape);
		bitmapData.applyFilter(
			bitmapData, bitmapData.rect, new Point(), new GlowFilter(0xffffff, 1, 2, 2, innerStrength, BitmapFilterQuality.HIGH)
		);
		return bitmapData;
	}
}